//spliter object

function MRHorizontalSpliter(){
    var _this = this;
    var $left = this.getElement('div.mrLeftSpliter');
    var $right = this.getElement('div.mrRightSpliter');
    
    //insert a div between divs which will be used to resize them
    var $bar = new Element('div', {
        'class':'mrHorizontalSpliterBar'
    }).inject($left, 'after');
    
    var minLeft = parseInt(this.get('data-minLeft')) || 0;
    var minRight = parseInt(this.get('data-minRight')) || 0;
    minRight = minRight + 5;
    var ll = Math.max($left.getSize().x, minLeft);
    
    $left.setStyles({
        top:'0',
        bottom: '0',
        left: '0',
        width: ll
    });
    
    $bar.setStyles({
        top: '0',
        bottom: '0',
        left: ll,
        width:'5px'
    });
    
    $right.setStyles({
        top: '0',
        bottom: '0',
        left: ll+5,
        right:'0'
    });
    
    //spliter methods
    this.moveTo = function(x){
        if(isNaN(x)) return;
        if(x < minLeft || x > this.getSize().x - minRight) return;
        //console.log(x);
        $bar.setStyle('left', x);
        $left.setStyle('width', x);
        $right.setStyle('left', (x + 5));
    };

    this.setMinLeft = function(value){
        if(isNaN(value)) return;
        if(value >= 0 && value < this.getSize().x){
            minLeft = value;
        }
    };
    
    this.setMinRight = function(value){
        if(isNaN(value)) return;
        if(value >= 0 && value < this.getSize().x){
            minRight = value + 5;
        }
    };
    
    this.getSpliterPossition = function(){
        return $left.getSize().x;
    }
    
    //Helpers
    var pos = null;
    
    function mouseMove (e){
        //console.log('pos: ' + pos.x + ' client: ' + e.client.x);
        _this.moveTo(e.client.x + pos.x);
        //console.log($bar.getPosition().x);
        return false;
    }
    
    function mouseUp (e){
        $bar.removeEvents('mousemove', mouseMove);
        $bar.removeEvents('mouseup', mouseUp);
        document.removeEvents('mousemove', mouseMove);
        document.removeEvents('mouseup', mouseUp);
    }
    
    $bar.addEvent('mousedown', function(e){
        var origin = $bar.getPosition(_this);
        //console.log('origin: ' + origin.x + ' client: ' + e.client.x + ' page: ' + e.page.x);
        pos = {x: origin.x - e.client.x, y: origin.y - e.client.y};
        //console.log(pos);
        $bar.addEvent('mousemove', mouseMove);
        document.addEvent('mousemove', mouseMove);
        $bar.addEvent('mouseup', mouseUp);
        document.addEvent('mouseup', mouseUp);
        return false;
    });
    
    return this;
};
/*
function MRVerticalSpliter(divZone, minTop, minBottom){
    var _this = this;
    var $divZone = $(divZone);
    var $top = $divZone.children('.mrTopSpliter');
    var $bottom = $divZone.children('.mrBottomSpliter');
    //insert a div between divs which will be used to resize them
    var $bar = $('<div class="mrVerticalSpliterBar"></div>');
    $top.after($bar);
    
    if(!minBottom) minBottom = 0;
    if(!minTop) minTop = 5;
    
    $bottom.css('bottom', 0);
    var bt = Math.max($bottom.height(), minBottom);
    $bottom.height(bt);
    $bar.css('bottom', bt);
    $top.css('bottom', bt + 5);
    $top.css('top', 0);
    
    $top.css('left', 0);
    $top.css('right', 0);
    $bar.css('left', 0);
    $bar.css('right', 0);
    $bottom.css('left', 0);
    $bottom.css('right', 0);
    
    var pos = null;
    
    this.debbug = function(){};
    
    function mouseMove (e){
        _this.debbug();
        _this.moveTo(e.clientY + pos.y);
    }
    
    function mouseUp (e){
        $bar.off('mousemove', mouseMove);
        $(document).off('mousemove', mouseMove);
        $bar.off('mouseup', mouseUp);
        $(document).off('mouseup', mouseUp);
    }
    
    $bar.mousedown(function(e){
        var origin = $(this).position();
        pos = {x: origin.left - e.clientX, y: origin.top - e.clientY};
        $(this).on('mousemove', mouseMove);
        $(document).on('mousemove', mouseMove);
        $(this).on('mouseup', mouseUp);
        $(document).on('mouseup', mouseUp);
        e.stopPropagation();
        return false;
    });
    
    this.moveTo = function(y){
        var h = $divZone.height();
        if(y < minTop || y > h - Math.max(minBottom, 0)) return;
        $bottom.height(h - y - 5);
        $bar.css('bottom', h - y - 5);
        $top.css('bottom', h - y);// + 5);
    };

    this.setMinTop = function(value){
        if(value >= 0 && value < $divZone.height()){
            minTop = value;
        }
    };
    
    this.setMinBottom = function(value){
        if(value >= 0 && value < $divZone.height() - 5){
            minBottom = value;
        }
    };
};
*/

window.addEvent('domready', function(){
    $$('div.mrHorizontalSpliter').each(function(h){
        MRHorizontalSpliter.bind(h)();
    });
});

/*$(function(){

    $('.mrHorizontalSpliter').each(function(){
        var a = new MRHorizontalSpliter(this, $(this).attr('data-minLeft'), $(this).attr('data-minRight'));
        if($(this).attr('id')){
            mrObjs[$(this).attr('id')] = a;
        }
    });
    
    $('.mrVerticalSpliter').each(function(){
        var a = new MRVerticalSpliter(this, $(this).attr('data-minTop'), $(this).attr('data-minBottom'));
        if($(this).attr('id')){
            mrObjs[$(this).attr('id')] = a;
        }
    });
});*/
